<?php 
/**
 * 用户注册页面
 */
if(!defined('EMLOG_ROOT')) {exit('error!');} 
?>
<?php
if(ROLE == 'admin' || ROLE == 'writer'){
	header('Location:'.BLOG_URL.'admin/');
}
global $CACHE;
$options_cache = $CACHE->readCache('options');
$DB = MySql::getInstance();
$blogname = $options_cache['blogname'];
$bloginfo = $options_cache['bloginfo'];
$site_title = $options_cache['blogname'];
$site_title = '注册会员 - '.$blogname;
$site_description = $options_cache['bloginfo'];
$site_key = $options_cache['site_key'];
$log_title = '注册会员';
$comments = array('commentStacks'=>array(), 'commentPageUrl'=>'');

$username = isset($_POST['username']) ? addslashes(trim($_POST['username'])) : '';
$password = isset($_POST['password']) ? addslashes(trim($_POST['password'])) : '';
$password2 = isset($_POST['password2']) ? addslashes(trim($_POST['password2'])) : '';
$imgcode = isset($_POST['imgcode']) ? strtoupper(addslashes(trim($_POST['imgcode']))): '';

if($username && $password && $password2 ){
	$sessionCode = isset($_SESSION['code']) ? $_SESSION['code'] : '';
	echo $sessionCode;
		$User_Model = new User_Model();
		if(!$User_Model -> isUserExist($username)){
			$hsPWD = new PasswordHash(8, true);
			$password = $hsPWD->HashPassword($password);
			$User_Model->addUser($username, $password, 'writer', 'y');
			$CACHE->updateCache();
			echo'<script>alert("友情提示:你已成功注册！"); window.location.href="'.BLOG_URL.'signin.html"</script>';
		}else{
			echo'<script>alert("友情提示:你输入的用户名已被占用！");</script>';
		}
	
	
};?>
  <section id="content" class="m-t-lg wrapper-md animated fadeInDown bg-info dker">
    <div class="container aside-xl">
      <!--<a class="navbar-brand block" href="<?php echo BLOG_URL; ?>"><span class="h1 font-bold">Musik</span></a>-->
      <section class="m-b-lg">
        <header class="wrapper text-center">
          <strong>Sign up to find interesting thing</strong>
        </header>
        <form action="" method="post" name="reg" id="reg" onsubmit="return checkReg();">
          <div class="form-group">
            <input placeholder="Name/Email" name="username" class="form-control rounded input-lg text-center no-border">
          </div>
          <div class="form-group">
            <input name="password" type="password" placeholder="Password" class="form-control rounded input-lg text-center no-border">
          </div>
          <div class="form-group">
             <input name="password2" type="password" placeholder="Repeat password" class="form-control rounded input-lg text-center no-border">
          </div>
          
		  <!--<div class="form-group">
             <input name="imgcode" type="text" placeholder="Verification code" class="form-control rounded input-lg text-center no-border">
          </div>
		  <!--<div class="checkbox i-checks m-b">
            <label class="m-l">
			  <a id="imginfo">更换验证码：</a>
			  <img src="<?php echo BLOG_URL; ?>include/lib/checkcode.php" width="80" id="yzcode" / align="center">
            </label>
          </div>-->
          <button type="submit" class="btn btn-lg btn-warning lt b-white b-2x btn-block btn-rounded"><i class="icon-arrow-right pull-right"></i><span class="m-r-n-lg">Sign up</span></button>
		  <button type="reset" class="btn btn-lg btn-warning lt b-white b-2x btn-block btn-rounded"><i class="icon-arrow-right pull-right"></i><span class="m-r-n-lg">重置</span></button>		  
          <div class="line line-dashed"></div>
          <p class="text-muted text-center"><small>Already have an account?</small></p>
          <a href="<?php echo BLOG_URL; ?>signin.html" class="btn btn-lg btn-info btn-block btn-rounded">Sign in</a>
        </form>
      </section>
    </div>
<script type="text/javascript">
function checkReg(){
	var usrName = $("input[name=username]").val().replace(/(^\s*)|(\s*$)/g, "");
	var pwd = $("input[name=password]").val().replace(/(^\s*)|(\s*$)/g, "");
	var pwd2 = $("input[name=password2]").val().replace(/(^\s*)|(\s*$)/g, "");
	var yzm = $("input[name=imgcode]").val().replace(/(^\s*)|(\s*$)/g, "");

	if(usrName.match(/\s/) || pwd.match(/\s/)){
		alert("用户名和密码中不能有空格");
		return false;
	}

	if(usrName == '' || pwd == '' || yzm == ''){
		alert("用户名、密码、验证码都不能为空！");
		return false;
	}
	if(usrName.length < 5 || pwd.length < 5){
		alert("用户名和密码都不能小于5位！");
		return false;
	}
	else if(pwd != pwd2){
		alert("两次输入密码不相等！");
		return false;
	}
}
$(function(){
	$("#imginfo").click(function(){
		//alert('haha');
		$("img#yzcode").attr("src", "<?php echo BLOG_URL;?>include/lib/checkcode.php?"+Math.random());
	});
})

</script>
  <!-- footer -->
  <footer id="footer">
    <div class="text-center padder clearfix">
      <p>
        <small>MV video download site response type<br>
		Powered by <a href="http://www.emlog.net" title="采用emlog系统">emlog</a> 
	    <a href="http://www.miibeian.gov.cn" target="_blank"><?php echo $icp; ?></a> <?php echo $footer_info; ?>
	    &copy; 2014</small>
      </p>
    </div>
  </footer>
  <!-- / footer -->
  </section>
  <script src="<?php echo TEMPLATE_URL; ?>js/jquery.min.js"></script>
  <!-- Bootstrap -->
  <script src="<?php echo TEMPLATE_URL; ?>js/bootstrap.js"></script>
  <!-- App -->
  <script src="<?php echo TEMPLATE_URL; ?>js/app.js"></script>  
  <script src="<?php echo TEMPLATE_URL; ?>js/slimscroll/jquery.slimscroll.min.js"></script>
  <script src="<?php echo TEMPLATE_URL; ?>js/app.plugin.js"></script>
</body>
</html>